package com.sccl.es.controller;

import com.sccl.es.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author tangyang
 * @version 1.0
 * @date 2021/5/11 15:08
 */
@RestController
public class ContentController {

    @Autowired
    private ContentService contentService;

    //爬取数据存入es
    @GetMapping("/parse/{keyword}")
    public Boolean parse(@PathVariable("keyword") String keyword) {
        Boolean flag = false;
        try {
            flag = contentService.parseContent(keyword);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    //搜索数据
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public List<Map<String,Object>> search(@PathVariable("keyword") String keyword,
                                           @PathVariable("pageNo") int pageNo,
                                           @PathVariable("pageSize") int pageSize) {
        List<Map<String,Object>>  list = new ArrayList<Map<String,Object>>();
        try {
            list = contentService.searchPage(keyword,pageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //搜索文本关键字高亮数据
    @GetMapping("/searchHigh/{keyword}/{pageNo}/{pageSize}")
    public List<Map<String,Object>> searchHigh(@PathVariable("keyword") String keyword,
                                           @PathVariable("pageNo") int pageNo,
                                           @PathVariable("pageSize") int pageSize) {
        List<Map<String,Object>>  list = new ArrayList<Map<String,Object>>();
        try {
            list = contentService.searchPageHigh(keyword,pageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
